package com.huawei.hms.framework.network.restclient.hwhttp.route;

import android.text.TextUtils;
import com.huawei.hms.framework.common.ContainerUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.common.PLSharedPreferences;
import com.huawei.hms.framework.network.restclient.hwhttp.Request;
import com.huawei.hms.framework.network.util.ContextUtil;
import java.util.LinkedHashSet;
import java.util.concurrent.ConcurrentHashMap;

public class HostRoute {
    private static final String AIROUTE_CONFIG_ATTRIBUTES = "airoute_conf";
    private static final String LAUNCH_USED_DOMAIN = "launch_used_domain";
    private static final int LONG_INTERVAL = 1000;
    private static final int MAX_DOMAIN_ENTRIES = 5;
    private static final int MAX_TIMES = 3;
    private static final int SHORT_INTERVAL = 500;
    private static final String TAG = "HostRoute";
    private static final int THRESHOLD = 400;
    private static volatile HostRoute instance;
    private final Object domainLock = new Object();
    private ConcurrentHashMap<String, IntervalInfo> intervalMap = new ConcurrentHashMap();
    private String[] lastLaunchUsedDomain;
    private final LinkedHashSet<String> launchUsedDomain = new LinkedHashSet(5);
    private PLSharedPreferences sp = new PLSharedPreferences(ContextUtil.getContext(), AIROUTE_CONFIG_ATTRIBUTES);

    static class IntervalInfo {
        int goodTimes = 1;
        int interval = 500;

        IntervalInfo() {
        }
    }

    private HostRoute() {
        if (this.sp == null) {
            Logger.e(TAG, "init failed, fail to get SharedPreferences");
        }
    }

    public static HostRoute getInstance() {
        if (instance == null) {
            synchronized (HostRoute.class) {
                if (instance == null) {
                    instance = new HostRoute();
                }
            }
        }
        return instance;
    }

    Request setConnectionDynamicDelay(Request request) {
        IntervalInfo intervalInfo = (IntervalInfo) this.intervalMap.get(request.getUrl().getHost());
        if (intervalInfo == null || intervalInfo.interval == request.getConnectionAttemptDelay()) {
            Logger.v(TAG, "request not change: ");
            return request;
        }
        Logger.v(TAG, "  old delay time = " + request.getConnectionAttemptDelay() + " new delay time " + intervalInfo.interval);
        return request.newBuilder().connectionAttemptDelay(intervalInfo.interval).build();
    }

    void updateIntervalMap(long j, String str) {
        if (j > 0 && str != null) {
            if (this.intervalMap.get(str) == null) {
                this.intervalMap.put(str, new IntervalInfo());
            }
            IntervalInfo intervalInfo = (IntervalInfo) this.intervalMap.get(str);
            if (j > 400) {
                intervalInfo.interval = 1000;
                intervalInfo.goodTimes = 1;
            } else if (intervalInfo.interval != 1000 || intervalInfo.goodTimes >= 3) {
                intervalInfo.goodTimes = 1;
                intervalInfo.interval = 500;
            } else {
                intervalInfo.goodTimes++;
            }
        }
    }

    public String[] getLastLaunchUsedDomains() {
        if (this.lastLaunchUsedDomain == null && this.sp != null) {
            this.lastLaunchUsedDomain = this.sp.getString(LAUNCH_USED_DOMAIN, "").split(ContainerUtils.FIELD_DELIMITER);
        }
        if (this.lastLaunchUsedDomain == null || this.lastLaunchUsedDomain.length == 0) {
            return new String[0];
        }
        return (String[]) this.lastLaunchUsedDomain.clone();
    }

    public void processDomains(String str) {
        if (!TextUtils.isEmpty(str)) {
            processFirstUsedDomain(str);
        }
    }

    private void processFirstUsedDomain(String str) {
        synchronized (this.domainLock) {
            if (this.launchUsedDomain.size() < 5) {
                this.launchUsedDomain.add(str);
                persistFirstUsedDomain();
            }
        }
    }

    private void persistFirstUsedDomain() {
        if (this.sp == null) {
            Logger.e(TAG, "fail to get valid SharedPreferences");
        } else {
            this.sp.edit().putString(LAUNCH_USED_DOMAIN, ContainerUtils.toString(this.launchUsedDomain)).apply();
        }
    }
}
